package lyj.springcloud.globalhystrix;


import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 配置一个全局通用的服务降级方法：
 *      1，添加注解@DefaultProperties
 *      2，编写defaultFallback属性指定的方法
 *
 *  @DefaultProperties(defaultFallback = "globalFallbackMethod")
 */
@DefaultProperties(defaultFallback = "",commandProperties = {
        @HystrixProperty(name = "execution.isolationglobalFallbackMethod.thread.timeoutInMilliseconds",value = "1500")
})
@RestController
public class GlobalHystrixController {

    @Value("${server.port}")
    private String port;


    /**
     *  注解@HystrixCommand属性fallbackMethod没有指定服务降级方法，则使用全局方法降级方法
     * @param id
     * @return
     */
    @GetMapping("/order/global/{id}")
    @HystrixCommand
    public String global_error(@PathVariable String id) {
//        int i = 10/0;
        try {
            TimeUnit.MILLISECONDS.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Thread.currentThread().getName() + "端口 》》 " + port;
    }

    /**
     * 全局服务降级兜底方法
     * @return
     */
    public String globalFallbackMethod()  {
        return Thread.currentThread().getName() + "全局服务降级方法执行》》》 " ;

    }
}
